/*
 * @Description: 仿真相关
 * @Date: 2022-04-02 11:21:04
 * @Author: yeming
 */

import { fromLonLat, transform } from 'ol/proj.js'
import { Point } from '../model/point.js'
import { Polygon } from '../model/polygon.js'
import { removeFeatureAndDeleteHashMap, removeAllFeatureAndDeleteHashMap } from '../common/removeFeature.js'
import { createRectangle } from '../common/geometry.js'

const SIMULATION_TAKE_OFF = './static/olcsMap/img/simulation_takeoff.png'
const SIMULATION_CENTER = './static/olcsMap/img/simulation_center.png'

class Simulation {
  constructor(ol2d, takeOffPointHashMap, centerPointHashMap, polygonHashMap, vectorSource) {
    this.ol2d = ol2d
    this.takeOffPointHashMap = takeOffPointHashMap
    this.centerPointHashMap = centerPointHashMap
    this.polygonHashMap = polygonHashMap
    this.vectorSource = vectorSource
  }

  /**
   * @desc: 添加起飞点(olcs rend3d = true 同步处理，暂不添加3d Entity)
   * @param {Number} sessionId
   * @param {Array} latlng
   */
  addTakeOffPoint (sessionId, latlng, image) {
    image = image != '' && image ? image : SIMULATION_TAKE_OFF

    let currentPointMap = this.takeOffPointHashMap.get(sessionId)
    if (currentPointMap == undefined) {
      let point = new Point(this.ol2d, this.vectorSource)
      point.addSinglePoint(sessionId, latlng, 10, 0, image, false, null, true)
      this.takeOffPointHashMap.set(sessionId, { 'item2d': point, 'item3d': null }) // 已做3d同步 rend3d = true item3d = null
    } else {
      this.updateTakeOffPointPosition(sessionId, latlng)
    }
  }

  /**
   * @desc: 更新起飞点
   * @param {Number} sessionId
   * @param {Array} latlng
   */
  updateTakeOffPointPosition (sessionId, latlng) {
    let currentPoint = this.takeOffPointHashMap.get(sessionId).item2d
    // 2d点
    let coordinate = fromLonLat(latlng)
    let feature = currentPoint.feature
    if (feature != null) feature.getGeometry().setCoordinates(coordinate)
    // 3d
    // let currentPoint3d = this.takeOffPointHashMap.get(sessionId).item3d
    // let position = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2] || 80)
    // currentPoint3d.position = position
  }

  /**
   * @desc: 删除起飞点
   * @param {Number} sessionId
   */
  delTakeOffPoint (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.takeOffPointHashMap, this.vectorSource)
  }

  /**
   * @desc: 删除所有起飞点
   */
  delAllTakeOffPoint () {
    removeAllFeatureAndDeleteHashMap(this.takeOffPointHashMap, this.vectorSource)
  }

  addAreaCenterPoint (sessionId, latlng, width, height, image) {
    image = image != '' && image ? image : SIMULATION_CENTER

    let currentPointMap = this.centerPointHashMap.get(sessionId)
    if (currentPointMap == undefined) {
      let point = new Point(this.ol2d, this.vectorSource)
      point.addSinglePoint(sessionId, latlng, 9, 0, image, false, null, true)
      this.centerPointHashMap.set(sessionId, { 'item2d': point, 'item3d': null }) // 已做3d同步 rend3d = true item3d = null

      let rectangleLatlng = createRectangle(latlng, width, height)
      let polygon = new Polygon(this.ol2d, this.vectorSource)
      this.polygonHashMap.set(sessionId, { 'item2d': polygon, 'item3d': null })
      polygon.renderSimulationPolygon(sessionId, [rectangleLatlng], width, height)
    } else {
      this.updateAreaCenterPointPosition(sessionId, latlng, width, height)
    }
  }

  updateAreaCenterPointPosition (sessionId, latlng, width, height) {
    // 中心点
    let currentPoint = this.centerPointHashMap.get(sessionId).item2d
    // 2d点
    let coordinate = fromLonLat(latlng)
    let feature = currentPoint.feature
    if (feature != null) feature.getGeometry().setCoordinates(coordinate)

    // 矩形区域
    let currentPolygon = this.polygonHashMap.get(sessionId).item2d
    let polygonWidth = width || currentPolygon.feature.getProperties()['width']
    let polygonHeight = height || currentPolygon.feature.getProperties()['height']
    let rectangleLatlng = createRectangle(latlng, polygonWidth, polygonHeight)

    let currentGeomPolygon = currentPolygon.polygonArea
    let currentCoordinates = currentGeomPolygon.getCoordinates()
    rectangleLatlng.forEach((item, index) => {
      currentCoordinates[0][index] = transform(item, "EPSG:4326", "EPSG:3857")
    })
    currentGeomPolygon.setCoordinates(currentCoordinates)
  }

  delAreaCenterPoint (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.centerPointHashMap, this.vectorSource)
    removeFeatureAndDeleteHashMap(sessionId, this.polygonHashMap, this.vectorSource)
  }
}

export default Simulation
